/**
 * 费用开票
 */
(function(vc){
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10; 
   vc.extends({
        data: {
            openInvoiceInfo: {
                tabs: [{
                    code: 'sellerInfo',
                    text: '销方信息'
                },{
                    code: 'detailSetting',
                    text: '明细设置'
                },{
                    code: 'redInvoice',
                    text: '冲红发票'
                }],
                activeTab: 'makeoutInvoice',
                queryForm: {
                    sellerName: '',
                    sellerTaxNo: '',
                    sellerPhone: ''
                },
                detailSettingForm: {
                    id: '',
                    invoiceItemName: '',
                    expenseName: ''
                },
                makeoutInvoiceForm: {
                    purchaserName: '',
                    purchaserPhone: '',
                    purchaserTaxNo: '',
                    purchaserAddress: '',
                    purchaserBranchBankAccount: '',
                    purchaserAddress: '',
                    sellerName: '',
                    sellerTaxNo: '',
                    sellerPhone: '',
                    sellerAddress: '',
                    sellerBranchBankAccount: '', // 销方开户行及账号
                    reviewer: '', // 复核人
                    invoiceClerk: '', // 开票员，
                    payee: '', // 收款人
                    invoiceType: '', // 发票种类
                    msgPushType: '', // 推送方式
                    remark: ''
                },
                hasOpenInvoiced: false, // 是否已开票标识
                sellerData: [],
                detailSettings: []
            }
        },
        _initMethod: function(){

        },
        _initEvent: function(){
            var invoiceApplyItem = JSON.parse(sessionStorage.getItem('invoiceApply'));
            sessionStorage.removeItem('invoiceApply');
            $that.openInvoiceInfo.makeoutInvoiceForm.purchaserName=invoiceApplyItem.ownerName;
            $that.openInvoiceInfo.makeoutInvoiceForm.purchaserPhone=invoiceApplyItem.applyTel;
            $that.openInvoiceInfo.makeoutInvoiceForm.purchaserTaxNo=invoiceApplyItem.invoiceNum;
            $that.openInvoiceInfo.makeoutInvoiceForm.purchaserAddress=invoiceApplyItem.invoiceAddress;
            var name = vc.getData('/nav/getUserInfo').name;
            $that.openInvoiceInfo.makeoutInvoiceForm.reviewer = name;
            $that.openInvoiceInfo.makeoutInvoiceForm.invoiceClerk = name;
            $that.openInvoiceInfo.makeoutInvoiceForm.payee = name;
            $that._listSellerInfo($that.openInvoiceInfo.queryForm);
            vc.on('reloadSellerData', 'list', function(param){
                $that._listSellerInfo(param);
            });
            $that._listDetailSettings({});
            vc.on('reloadDetailSetting', 'list', function(param){
                $that._listDetailSettings(param);
            });
        },
        methods: {
            switchTab: function(code){
                $that.openInvoiceInfo.activeTab = code;
            },
            sellerNameChange: function(e){
                var selectedIndex = e.target.value;
                var selectedItem = $that.openInvoiceInfo.sellerData[selectedIndex];
                // $that.openInvoiceInfo.makeoutInvoiceForm.sellerName = selectedItem.sellerName;
                $that.openInvoiceInfo.makeoutInvoiceForm.sellerTaxNo = selectedItem.sellerTaxNo;
                $that.openInvoiceInfo.makeoutInvoiceForm.sellerAddress = selectedItem.sellerAddress;
                $that.openInvoiceInfo.makeoutInvoiceForm.sellerPhone = selectedItem.sellerPhone;
                $that.openInvoiceInfo.makeoutInvoiceForm.sellerBranchBankAccount = selectedItem.sellerBranchBank + selectedItem.sellerBankAccount;
            },
            _listSellerInfo: function(param){
                var url = '/invoice.listSellerInfo';
                if(!param.page){
                    param.page = DEFAULT_PAGE;
                }
                if(!param.row){
                    param.row = DEFAULT_ROWS;
                }
                url+='?page='+param.page+'&row='+param.row+'&sellerName='+param.sellerName+'&sellerTaxNo='+param.sellerTaxNo+'&sellerPhone='+param.sellerPhone;
                vc.http.apiGet(url, param, 
                    function(json,res){
                        let _json = JSON.parse(json);
                        if(_json.code==0){
                            $that.openInvoiceInfo.sellerData = _json.data
                        }else{
                            vc.toast('获取数据失败')
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理:'+errInfo);
                    }
                )

            },
            _listDetailSettings: function(param){
                var url = '/invoice.listDetailSetting';
                url+='?page='+DEFAULT_PAGE+'&row='+DEFAULT_ROWS;
                if(param.id){
                    url+='&id='+param.id;
                }
                if(param.invoiceItemName){
                    url += "&invoiceItemName="+param.invoiceItemName;
                }
                if(param.expenseName){
                    url+='&expenseName='+param.expenseName;
                }
                vc.http.apiGet(url, param, 
                    function(json, res){
                        let _json = JSON.parse(json);
                        if(_json.code==0){
                            $that.openInvoiceInfo.detailSettings = _json.data
                        }else{
                            vc.toast('获取数据失败')
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理:'+errInfo);
                    }
                )
            },
            _querySellerInfo: function(){
               var param =  $that.openInvoiceInfo.queryForm;
               $that._listSellerInfo(param);
            },
            _editSellerInfo: function(item){
                vc.emit('invoiceSellerInfo', 'sellerInfoModal', item);
            },
            _deleteSellerInfo: function(oiId){
                vc.emit('invoiceSeller','openDelInvoiceSellerModal', {oiId: oiId});
            },
            _addSellerInfo: function(){
                vc.emit('invoiceSellerInfo', 'sellerInfoModal', {});
            },
            _queryDetailSettings: function(){
                debugger;
                var param = $that.openInvoiceInfo.detailSettingForm;
                $that._listDetailSettings(param);
            },
            _addDetailSetting: function(){
                debugger;
                vc.emit('invoiceDetailSetting', 'openModal', {});
            },
            _editDetailSetting: function(item){
                vc.emit('invoiceDetailSetting', 'openModal', item);
            },
            _deleteDetailSetting: function(id){
                console.log(id);
            },
            _submitOpenInvoice: function(){

            }
        }
   });
})(window.vc)